package com.gugee.ins.common.dal.service.media.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gugee.ins.common.dal.mapper.media.InsMediaExtMapper;
import com.gugee.ins.common.dal.service.media.InsMediaDao;
import com.gugee.ins.common.dto.InsAdMediaStInfoDto;
import com.gugee.ins.common.dto.InsMediaScoreDto;
import com.gugee.ins.common.model.es.media.InsMediaIndex;
import com.gugee.ins.common.model.mysql.media.InsMedia;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * insBlogger dao
 * @author Huangxin
 */
@Slf4j
@Service
public class InsMediaDaoImpl extends ServiceImpl<InsMediaExtMapper,InsMedia> implements InsMediaDao {

    /**
     * 更新帖子记录中评论相关字段
     * @param shortcode
     * @param collectCommentCount
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMediaCommentInfo(String shortcode,long collectCommentCount) {
        baseMapper.updateMediaCommentInfo(collectCommentCount,System.currentTimeMillis() / 1000,shortcode);
    }

    /**
     * 更新帖子是否是广告帖
     * @param shortcode
     * @param isAd
     */
    @Override
    public void updateMediaIsAd(String shortcode,int isAd){
        baseMapper.updateMediaIsAd(isAd,shortcode);
    }

    /**
     * 根据 uid 和 时间 查找数据
     * @param uid
     * @param timeLimit
     * @param commentMinCount
     * @return
     */
    @Override
    public List<InsMedia> findByOwnerUidAndTakenTime(long uid, long timeLimit,int commentMinCount){
        return baseMapper.findByOwnerUidAndTakenTime(uid,timeLimit,commentMinCount);
    }

    @Override
    public List<InsMediaIndex> findInsMediaIndexByOwnerUidAndTakenTime(long uid, long timeLimit) {
        return baseMapper.findInsMediaIndexByOwnerUidAndTakenTime(uid,timeLimit);
    }

    /**
     * 根据 shortcode 查找数据
     * @param shortcode
     * @return
     */
    @Override
    public InsMedia findByShortcode(String shortcode){
        QueryWrapper<InsMedia> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("shortcode",shortcode);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 统计帖子数
     * @param uid
     * @param timeLimit
     * @param isAd
     * @return
     */
    @Override
    public long countByOwnerUidAndTakenTimeAndAdIs(long uid,long timeLimit, Integer isAd){
        return baseMapper.countByOwnerUidAndTakenTimeAndAdIs(uid,timeLimit,isAd);
    }

    @Override
    public double getAdMediaRate(long uid, long timeLimit) {
        return baseMapper.getAdMediaRate(uid,timeLimit);
    }

    /**
     * 统计参与率
     * @param uid
     * @param timeLimit
     * @param isAd
     * @return
     */
    @Override
    public double sumERByOwnerUidAndTakenTimeAndAdIs(long uid,long timeLimit, Integer isAd){
        return baseMapper.sumERByOwnerUidAndTakenTimeAndAdIs(uid,timeLimit,isAd);
    }

    @Override
    public double avgERByOwnerUidAndTakenTimeAndAdIs(long uid, long timeLimit, Boolean isAd) {
        return baseMapper.avgERByOwnerUidAndTakenTimeAndAdIs(uid,timeLimit,isAd);
    }

    /**
     * 根据yid 获取标题文本
     * @param uid
     * @param limitMinLength
     * @return
     */
    @Override
    public List<String> findCaptionByUid(long uid,int limitMinLength){
        return baseMapper.findCaptionByUid(uid,limitMinLength);
    }

    /**
     * 根据uid 获取需要进行广告帖识别的shortcode
     * @param uid
     * @param limitTime
     * @return
     */
    @Override
    public List<String> findAdMediaIdentifyShortCodeByUid(long uid,long limitTime){
        return baseMapper.findAdMediaIdentifyShortCodeByUid(uid,limitTime);
    }

    /**
     * 近30天帖子总数
     * @param uid
     * @param timeLimit
     * @return
     */
    @Override
    public int getMediaTotal(long uid,long timeLimit){
        return baseMapper.getMediaTotal(uid,timeLimit);
    }

    /**
     * 广告帖子分类统计信息
     * @param uid
     * @param timeLimit
     * @return
     */
    @Override
    public List<InsAdMediaStInfoDto> getAdMediaInfoByUid(long uid, long timeLimit){
        return baseMapper.getAdMediaInfoByUid(uid,timeLimit);
    }

    /**
     * 近一周广告帖子统计信息
     * @param uid
     * @param timeLimit
     * @return
     */
    @Override
    public InsAdMediaStInfoDto getAdMediaInfoForWeekByUid(long uid, long timeLimit){
        return baseMapper.getAdMediaInfoForWeekByUid(uid,timeLimit);
    }

    /**
     * 根据shortcode 更新带货状态
     * @param shortcode
     */
    @Override
    public void updateProductTagType(String shortcode) {
        baseMapper.updateProductTagType(shortcode);
    }

    /**
     * 根据uid 和 时间区间获取需要计算分数的近期帖子列表
     * @param uid
     * @param limitTime
     * @return
     */
    @Override
    public List<InsMediaScoreDto> findAnalysisScoreMediaByUid(long uid, long limitTime) {
        return baseMapper.findAnalysisScoreMediaByUid(uid,limitTime);
    }

    /**
     * 批量更新 帖子分数
     * @param insMediaScoreDtoList
     */
    @Override
    public void batchUpdateScore(List<InsMediaScoreDto> insMediaScoreDtoList) {
        long now = System.currentTimeMillis() / 1000;
        try{
            baseMapper.batchUpdateScore(insMediaScoreDtoList,now);
        }catch (Exception e){
            log.debug("batch Update ins media Analysis score failed ,try Update one by one, msg : {} ",e.getMessage());
            insMediaScoreDtoList.forEach(insMediaScoreDto -> {
                try{
                    baseMapper.updateScore(insMediaScoreDto.getId(),insMediaScoreDto.getScore(),now);
                }catch (Exception ex){
                    e.printStackTrace();
                    log.info("Update ins media Analysis score failed ,id : {},score : {} , msg : {} "
                            ,insMediaScoreDto.getId(),insMediaScoreDto.getScore(),ex.getMessage());
                }
            });
        }
    }
}
